package taskTracker.ui.lwuit;

import taskTracker.Project;

import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.layouts.BoxLayout;

public class ProjectForm extends Form {
	/**Command for accepting changes*/
	public final Command cmdOk = new Command("OK");
	
	/**Command to cancel changes*/
	public final Command cmdCancel = new Command("Cancel");
	
	/**Field to set Project name*/
	private final TextField nameField = new TextField();
	/**Field to set Project notes*/
	private final TextArea noteField = new TextArea();
	/**Field to set Project tags*/
	private final TextArea tagField = new TextArea();
	
	/**The Project that is currently being edited*/
	private Project project;

	/**
	 * Initializes the dialog from a task list.
	 * If the given parameter is null, list is initialized for a new Project
	 * @param project
	 */
	public ProjectForm() {
		super.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
		super.addCommand(cmdOk);
		super.addCommand(cmdCancel);
		Label label;
		super.addComponent(label = new Label("Name"));
		super.addComponent(nameField);
		nameField.setLabelForComponent(label);
		super.addComponent(label = new Label("Notes"));
		super.addComponent(noteField);
		noteField.setLabelForComponent(label);
		super.addComponent(label = new Label("Tags"));
		super.addComponent(tagField);
		tagField.setLabelForComponent(label);
		
	}

	/**
	 * Retrieves data of task list stored in this dialog
	 * @param project
	 * @return
	 */
	public Project getProject() {
		if (project == null)
			project = new Project(nameField.getText(), noteField.getText());
		project.setName(nameField.getText());
		project.setNotes(noteField.getText());
		String tagString = tagField.getText();
		project.setTags(tagString);
		return project;
	}
	
	/**
	 * Sets data from a task list.
	 * @param project
	 */
	public void setProject(Project project) {
		if (project == null) {
			super.setTitle("New list");
			nameField.setText("");
			noteField.setText("");
			tagField.setText("");
		} else {
			super.setTitle(project.getName());
			nameField.setText(project.getName());
			noteField.setText(project.getNotes());
			tagField.setText(project.getTagsAsString());
		}
		this.project = project;
	}

}
